<?php

class skinController extends Controller{
	
    public function skindata(){
    	//当前页
    	$paging = isset($_GET['page'])?$_GET['page']:1;
		$this->assign('paging',$paging);
		//一页显示多少个个数
		$pagesize = 8;
		//链接数据
        $skinModel = new skinModel();
		//英雄数据
        $skin = $skinModel->selectUser($paging,$pagesize);
        $this->assign('skin',$skin);
		//英雄数据的总页数
		$skinrow = $skinModel->countskin();
		//页面是显示多少个页
		$skinrows = ceil($skinrow / $pagesize);
        $this->assign('skinrows',$skinrows);
		//view显示页面
        $this->display('skin.tpl');
    }
	
	public function deleteskin(){
		 //获取当前页
		 
		 $skinid = isset($_GET['skinid'])?$_GET['skinid']:'';
		 $paging = $_GET['page'];
		 $pagesize = 8;
		 
         $skinModel = new skinModel();

         $deleteskin = $skinModel->deleteskin($skinid);

         if($deleteskin){
         	//英雄数据的总页数
			$skinrow = $skinModel->countskin();
			$skinrows = ceil($skinrow / $pagesize);
			if($paging>$skinrows){
				$paging=$skinrows;
			}
            echo json_encode(['code'=>1,'page'=>$paging]);
         }else{
             echo json_encode(['code'=>2]);
         }
    }
	public function addskin(){
		//获取数据
		$skin_id = $_POST['skin_id'];
		$skin_name = $_POST['skin_name'];
		$skin_price = $_POST['skin_price'];
		//获取英雄皮肤图片
		if(isset($_FILES['file']) && isset($_FILES['smfile'])){
			$file = $_FILES['file'];
			$smfile = $_FILES['smfile'];
			$suffixarr = ['jpg','png','gif','jpeg'];
			$filename = $file['name'];
			$smfilename = $smfile['name'];
			$namearr = explode('.', $filename);
			$smnamearr = explode('.', $smfilename);
			$suffix = end($namearr);
			$smsuffix = end($smnamearr);
		if((
				$file['type'] == 'image/jpeg' || 
				$file['type'] == 'image/jpg' ||
				$file['type'] == 'image/png' || 
				$file['type'] == 'image/gif'||
				$smfile['type'] == 'image/jpeg' || 
				$smfile['type'] == 'image/jpg' ||
				$smfile['type'] == 'image/png' || 
				$smfile['type'] == 'image/gif') &&
				$file['size'] < 1048576 && $smfile['size'] < 1048576 &&
				in_array($suffix, $suffixarr) && in_array($smsuffix, $suffixarr)
			){
				if($file['error']>0 && $smfilename['error']>0){
					echo json_encode(['code'=>203,'msg'=>'文件异常']);
				}else{
					$newname = time().rand(1, 100).".".$suffix;
					$smnewname = time().rand(1, 100).".".$smsuffix;
					$newpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$newname;
					$smnewpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$smnewname;
//					echo "$newpath";
					$rst = move_uploaded_file($file['tmp_name'], $newpath);
					$smrst = move_uploaded_file($smfile['tmp_name'], $smnewpath);
					if($rst && $smrst){
						$skin_skin="./public/img/".$newname;
						$skin_smskin="./public/img/".$smnewname;
						$pagesize = 8;
						$skinModel = new skinModel();
						//英雄数据的总页数
						$skinrow = $skinModel->countskin();
						$oldskinrows = ceil($skinrow / $pagesize);
						
				        $addskin = $skinModel->addskin($skin_id,$skin_skin,$skin_smskin,$skin_name,$skin_price);
						
				        if($addskin){
					        //英雄数据的总页数
							$skinrow = $skinModel->countskin();
							$skinrows = ceil($skinrow / $pagesize);
							if($oldskinrows<=$skinrows){
								$paging=$skinrows;
							}
				            echo json_encode(['code'=>1,'page'=>$paging]);
							
				        }else{
				             echo json_encode(['code'=>2,'msg'=>'未知原因']);
				        }
					}
				}
				}else{
				echo json_encode(['code'=>201,'msg'=>'文件格式不正确']);
			}
		}
		
       
    }
	public function modifyskin(){
		//获取数据
		$skin_id = $_POST['skin_id'];
		$modify_id = $_POST['modify_id'];
		$modify_name = $_POST['modify_name'];
		$modify_price = $_POST['modify_price'];
		//获取英雄皮肤图片
		if(isset($_FILES['file']) && isset($_FILES['smmodify'])){
			$file = $_FILES['file'];
			$smfile = $_FILES['smmodify'];
			$suffixarr = ['jpg','png','gif','jpeg'];
			$filename = $file['name'];
			$smfilename = $smfile['name'];
			$namearr = explode('.', $filename);
			$smnamearr = explode('.', $smfilename);
			$suffix = end($namearr);
			$smsuffix = end($smnamearr);
		if((
				$file['type'] == 'image/jpeg' || 
				$file['type'] == 'image/jpg' ||
				$file['type'] == 'image/png' || 
				$file['type'] == 'image/gif'||
				$smfile['type'] == 'image/jpeg' || 
				$smfile['type'] == 'image/jpg' ||
				$smfile['type'] == 'image/png' || 
				$smfile['type'] == 'image/gif') &&
				$file['size'] < 1048576 && $smfile['size'] < 1048576 &&
				in_array($suffix, $suffixarr) && in_array($smsuffix, $suffixarr)
			){
				if($file['error']>0 && $smfile['error']>0){
					echo json_encode(['code'=>203,'msg'=>'文件异常']);
				}else{
					$newname = time().rand(1, 100).".".$suffix;
					$smnewname = time().rand(1, 100).".".$smsuffix;
					$newpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$newname;
					$smnewpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$smnewname;
//					echo "$newpath";
					$rst = move_uploaded_file($file['tmp_name'], $newpath);
					$smrst = move_uploaded_file($smfile['tmp_name'], $smnewpath);
					if($rst && $smrst){
						$modify_skin="./public/img/".$newname;
						$modify_smskin="./public/img/".$smnewname;
						$skinModel = new skinModel();
				        $modifyskin = $skinModel->modifyskinimgs($modify_id,$modify_skin,$modify_smskin,$modify_name,$modify_price,$skin_id);
				        if($modifyskin){
				            echo json_encode(['code'=>1]);
				        }else{
				             echo json_encode(['code'=>2,'msg'=>'未知原因']);
				        }
					}
				}
				}else{
				echo json_encode(['code'=>201,'msg'=>'文件格式不正确']);
			}
		}else if(isset($_FILES['file']) && isset($_FILES['smmodify'])==FALSE){
			$file = $_FILES['file'];
			$suffixarr = ['jpg','png','gif','jpeg'];
			$filename = $file['name'];
			$namearr = explode('.', $filename);
			$suffix = end($namearr);
		if((
				$file['type'] == 'image/jpeg' || 
				$file['type'] == 'image/jpg' ||
				$file['type'] == 'image/png' || 
				$file['type'] == 'image/gif') &&
				$file['size'] < 1048576 &&
				in_array($suffix, $suffixarr)
			){
				if($file['error']>0){
					echo json_encode(['code'=>203,'msg'=>'文件异常']);
				}else{
					$newname = time().rand(1, 100).".".$suffix;
					$newpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$newname;
					$rst = move_uploaded_file($file['tmp_name'], $newpath);
					if($rst){
						$modify_skin="./public/img/".$newname;
						$skinModel = new skinModel();
				        $modifyskin = $skinModel->modifyskinimgone($modify_id,$modify_skin,$modify_name,$modify_price,$skin_id);
				        if($modifyskin){
				            echo json_encode(['code'=>1]);
				        }else{
				             echo json_encode(['code'=>2,'msg'=>'未知原因']);
				        }
					}
				}
				}else{
				echo json_encode(['code'=>201,'msg'=>'文件格式不正确']);
			}
		}else if(isset($_FILES['smmodify']) && isset($_FILES['file'])==FALSE){
			$smfile = $_FILES['smmodify'];
			$suffixarr = ['jpg','png','gif','jpeg'];
			$smfilename = $smfile['name'];
			$smnamearr = explode('.', $smfilename);
			$smsuffix = end($smnamearr);
		if((
				$smfile['type'] == 'image/jpeg' || 
				$smfile['type'] == 'image/jpg' ||
				$smfile['type'] == 'image/png' || 
				$smfile['type'] == 'image/gif') &&
				 $smfile['size'] < 1048576 &&
				 in_array($smsuffix, $suffixarr)
			){
				if($smfile['error']>0){
					echo json_encode(['code'=>203,'msg'=>'文件异常']);
				}else{
					$smnewname = time().rand(1, 100).".".$smsuffix;
					$smnewpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$smnewname;
					$smrst = move_uploaded_file($smfile['tmp_name'], $smnewpath);
					if($smrst){
						$modify_smskin="./public/img/".$smnewname;
						$skinModel = new skinModel();
				        $modifyskin = $skinModel->modifyskinimgtwo($modify_id,$modify_smskin,$modify_name,$modify_price,$skin_id);
				        if($modifyskin){
				            echo json_encode(['code'=>1]);
				        }else{
				             echo json_encode(['code'=>2,'msg'=>'未知原因']);
				        }
					}
				}
				}else{
				echo json_encode(['code'=>201,'msg'=>'文件格式不正确']);
			}
		}else{
			//没有皮肤图片传入
			$skinModel = new skinModel();
			
			$modifyskin = $skinModel->modifyskin($modify_id,$modify_name,$modify_price,$skin_id);
			
	        if($modifyskin){
	            echo json_encode(['code'=>1]);
	        }else{
	            echo json_encode(['code'=>2,'msg'=>'未知原因']);
	        }
		}
		
       
    }
	
}

?>